/*===============================================================
            write
 *   Copyright (C) 2017 All rights reserved.
 *   
 *   文件名称：main.c
 *   创 建 者：陈迪林
 *   创建日期：2017年08月02日
 *   描    述：
 *
 ================================================================*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include "lib.h"

char buff[100];
char *args[10];

int main(int argc,char *argv[]) 
{
    signal(SIGINT, SIG_IGN);/* 屏蔽ctrl+c */
    if(argc < 3)
    {
        printf("Usage: %s <IP> <Port>\n",argv[0]);
        return -1;
    }

    int cliensock = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == cliensock)
    {
        perror("Socket");
        return -1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);

    if(-1 == connect(cliensock, (struct sockaddr *)&addr, sizeof addr))
    {
        perror("Connect");
        return -1;
    }

    char nbuff[101];

    printf("[1] Login.\n");
    printf("[2] Register.\n");
    printf("[3] quit.\n");
    int option;
    int flag = 0;
    do
    {
        printf("----------------");fflush(stdout);
        scanf("%d",&option);
        char s;
        scanf("%c",&s);
        if(1 == option)
        {
            write(cliensock,"Login",100);
            if(0 == do_login(cliensock))
            {
                printf("Login successful!\n");
                flag = 1;
            }
            else
            {
                printf("Username or password error!\n");
                flag = 0;
            }
        }
        else if(2 == option)
        {
            write(cliensock,"Register",100);
            char p = do_register(cliensock);
            if('1' == p)
            {
                printf("Register successful!\n");
                flag = 1;
            }
            else if('2' == p)
            {
                printf("User name already exists!\n");
                flag = 0;
            }
            else 
            {
                printf("Register error\n");
                flag = 0;
            }
        }
        else if(3 == option)
        {
            write(cliensock,"quit",100);
  //          usleep(100000);
            close(cliensock);
            exit(0);
        }
        else 
        {
            printf("Please enter 1 or 2 or 3.\n");
            flag = 0;
        }
        
    }while(flag == 0);
 
    
    while(1)
    {
        printf("cmd->");fflush(stdout);

        gets(buff);

        nbuff[0] = ' ';
        memcpy(nbuff+1, buff, 100);
        fill_args(args, nbuff);

        if(0 == strncmp(buff, "get", 3))
        {
            write(cliensock,buff,100);
            do_get(cliensock);
        }
        else if(0 == strncmp(buff, "put", 3))
        {
            do_put(cliensock);
        }
        else if(0 == strncmp(buff, "ls", 2))
        {
            do_ls(cliensock);
        }
        else if(0 == strncmp(buff, "h", 1))
        {
            printf("---------------help------------\n");
            printf("get <filename> : download file.\n");
            printf("put <filename> : upload file.\n");
            printf("ls             : list file.\n");
            printf("h              : help.\n");
            printf("q              : quit.\n");
            printf("-------------------------------\n");
        }
        else if(0 == strncmp(buff, "q", 1))
        {
            write(cliensock, buff, 100);
            close(cliensock);
            exit(0);
        }
        else
        {
            printf("Unkown cmd! enter 'h' to help!\n");
        }
    }

    return 0;
}
